package com.xzzz.irda.guardcat.core.metric;

import cn.hutool.core.date.DateUtil;
import cn.hutool.system.oshi.OshiUtil;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.xzzz.common.base.util.SystemUtil;
import lombok.Data;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import oshi.hardware.GlobalMemory;

/**
 * 【Core】机器硬件指标
 *
 * @author xzzz
 */
@RestController
@RequestMapping("/guardcat/core/machine")
public class GuardcatMachineController {

    /**
     * 机器CPU与物理内存信息
     */
    @GetMapping("/cpuAndMem")
    public MachineMetric getMachineMetric() {
        MachineMetric machineMetric = new MachineMetric();
        GlobalMemory memory = OshiUtil.getMemory();
        machineMetric.setCreTime(DateUtil.now());
        machineMetric.setCpuUsed(0.0);
        machineMetric.setMemTotal(memory.getTotal());
        machineMetric.setMemAvailable(memory.getAvailable());

        return machineMetric;
    }

    /**
     * 机器名与IP
     */
    @GetMapping("/hostName")
    public MachineHostIp getHost() {
        MachineHostIp machineHostIp = new MachineHostIp();
        machineHostIp.setHostName(SystemUtil.getHostName());
        machineHostIp.setIp(SystemUtil.getIp());
        return machineHostIp;
    }

    /**
     * 机器信息
     */
    @Data
    @JsonInclude(JsonInclude.Include.NON_NULL)
    public static class MachineMetric {
        /**
         * CPU使用率
         */
        private Double cpuUsed;
        /**
         * 时间
         */
        private String creTime;
        /**
         * 总内存
         */
        private Long memTotal;
        /**
         * 剩余内存
         */
        private Long memAvailable;
    }

    @Data
    @JsonInclude(JsonInclude.Include.NON_NULL)
    public static class MachineHostIp {
        /**
         * 机器名称
         */
        private String hostName;
        /**
         * IP
         */
        private String ip;
    }
}
